package com.bw.demo_mvvm.net

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * @Author : ZhouZiYao
 * @Time : On 2023/11/16 19:24
 * @Description :
 */
class RetrofitManager {

    companion object{
        @JvmStatic
        val instance: RetrofitManager by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            RetrofitManager()
        }
    }

    private val retrofit: Retrofit = Retrofit.Builder()
        .baseUrl("http://10.161.9.80:7012")
        .client(createClient())
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(LiveDataCallAdapterFactory())
        .build()

    private fun createClient(): OkHttpClient {
        return OkHttpClient.Builder()
            .readTimeout(60,TimeUnit.HOURS)
            .writeTimeout(60,TimeUnit.HOURS)
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build()
    }

    fun <T> createService(service: Class<T>): T {
        return retrofit.create(service)
    }

}